package GUIs;

import Configs.Config;
import Controllers.ChiTietGiaController;
import Controllers.GiaPhongController;
import Controllers.LoaiPhongTroController;
import Controllers.MyTableFormat;
import Controllers.NhaTroController;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import models.NhaTro;

public class f_nhanvien_chunhatro_qlgiaphong extends javax.swing.JFrame {

    private static f_nhanvien_chunhatro_qlgiaphong _instance; //Kiểm tra sự tồn tại của 2 instance trong cùng 1 form
    private final Config conn;
    public NhaTro nhaTroThem;
    private NhaTroController nhaTroControl;
    private GiaPhongController giaPhongControl;
    private LoaiPhongTroController loaiPhongControl;
    private final MyTableFormat myFormat;
    private ChiTietGiaController chiTietGiaControl;
    private int rowEditLoaiPhong = -1;
    private int rowEditGiaPhong = -1;
    public String giaPhongOld = null;
    public String ngayApDungOld = null;
    public String loaiPhongOld = null;

    public f_nhanvien_chunhatro_qlgiaphong(Config conn) {
        this.conn = conn;
        initComponents();
        this.myFormat = new MyTableFormat();
        this.myFormat.formatTable(this.tbdsloaiphong);
        this.myFormat.formatTable(this.tbdsgiaphong);
    }

    public static f_nhanvien_chunhatro_qlgiaphong getInstance(Config conn, String soGPDKKD) {
        if (_instance == null) {
            _instance = new f_nhanvien_chunhatro_qlgiaphong(conn);
        }
        _instance.nhaTroControl = new NhaTroController(_instance.conn);
        _instance.loaiPhongControl = new LoaiPhongTroController(_instance.conn);
        _instance.giaPhongControl = new GiaPhongController(_instance.conn);
        _instance.chiTietGiaControl = new ChiTietGiaController(_instance.conn);
        _instance.nhaTroThem = _instance.nhaTroControl.getNhaTroWithGPKD(soGPDKKD);
        _instance.loaiPhongControl.setCbbLoaiPhong(_instance.cbbloaiphong);
        _instance.reloadContent();
        return _instance;
    }

    public void createAndShowUI() {
        setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popuploaiphong = new javax.swing.JPopupMenu();
        menusualoaiphong = new javax.swing.JMenuItem();
        menuxoaloaiphong = new javax.swing.JMenuItem();
        popupgiaphong = new javax.swing.JPopupMenu();
        menusuagiaphong = new javax.swing.JMenuItem();
        menuxoagiaphong = new javax.swing.JMenuItem();
        pnchinh = new javax.swing.JPanel();
        lbtieude = new javax.swing.JLabel();
        sptbdsloaiphong = new javax.swing.JScrollPane();
        tbdsloaiphong = new javax.swing.JTable();
        sptbdsgiaphong = new javax.swing.JScrollPane();
        tbdsgiaphong = new javax.swing.JTable();
        pnleft = new javax.swing.JPanel();
        lbidgiaphong = new javax.swing.JLabel();
        tfidgiaphong = new javax.swing.JTextField();
        lbgiaphong = new javax.swing.JLabel();
        lbloaiphong = new javax.swing.JLabel();
        cbbloaiphong = new javax.swing.JComboBox();
        lbngayapdung = new javax.swing.JLabel();
        dpngayapdung = new org.jdesktop.swingx.JXDatePicker();
        btnluugiaphong = new javax.swing.JButton();
        btnlamlaigiaphong = new javax.swing.JButton();
        tfgiaphong = new javax.swing.JFormattedTextField();
        pnright = new javax.swing.JPanel();
        lbidloaiphong = new javax.swing.JLabel();
        tfidloaiphong = new javax.swing.JTextField();
        lbmotaloaiphong = new javax.swing.JLabel();
        tfmotaloaiphong = new javax.swing.JTextField();
        lbsonguoio = new javax.swing.JLabel();
        cbbsonguoio = new javax.swing.JComboBox();
        btnluuloaiphong = new javax.swing.JButton();
        btnlamlailoaiphong = new javax.swing.JButton();

        menusualoaiphong.setText("Sửa thông tin loại phòng");
        menusualoaiphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menusualoaiphongActionPerformed(evt);
            }
        });
        popuploaiphong.add(menusualoaiphong);

        menuxoaloaiphong.setText("Xóa loại phòng");
        menuxoaloaiphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuxoaloaiphongActionPerformed(evt);
            }
        });
        popuploaiphong.add(menuxoaloaiphong);

        menusuagiaphong.setText("Sửa thông tin giá phòng");
        menusuagiaphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menusuagiaphongActionPerformed(evt);
            }
        });
        popupgiaphong.add(menusuagiaphong);

        menuxoagiaphong.setText("Xóa giá phòng");
        menuxoagiaphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuxoagiaphongActionPerformed(evt);
            }
        });
        popupgiaphong.add(menuxoagiaphong);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Quản lý giá phòng");
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);

        lbtieude.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lbtieude.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbtieude.setText("Quản lý giá phòng");

        tbdsloaiphong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "ID loại phòng", "Mô tả loại", "Số người ở"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbdsloaiphong.setComponentPopupMenu(popuploaiphong);
        tbdsloaiphong.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tbdsloaiphong.getTableHeader().setReorderingAllowed(false);
        sptbdsloaiphong.setViewportView(tbdsloaiphong);
        if (tbdsloaiphong.getColumnModel().getColumnCount() > 0) {
            tbdsloaiphong.getColumnModel().getColumn(0).setResizable(false);
            tbdsloaiphong.getColumnModel().getColumn(1).setResizable(false);
            tbdsloaiphong.getColumnModel().getColumn(2).setResizable(false);
            tbdsloaiphong.getColumnModel().getColumn(3).setResizable(false);
        }

        tbdsgiaphong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "ID giá", "Giá phòng", "Ngày áp dụng"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbdsgiaphong.setComponentPopupMenu(popupgiaphong);
        tbdsgiaphong.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tbdsgiaphong.getTableHeader().setReorderingAllowed(false);
        sptbdsgiaphong.setViewportView(tbdsgiaphong);
        if (tbdsgiaphong.getColumnModel().getColumnCount() > 0) {
            tbdsgiaphong.getColumnModel().getColumn(0).setResizable(false);
            tbdsgiaphong.getColumnModel().getColumn(1).setResizable(false);
            tbdsgiaphong.getColumnModel().getColumn(2).setResizable(false);
            tbdsgiaphong.getColumnModel().getColumn(3).setResizable(false);
        }

        pnleft.setBorder(javax.swing.BorderFactory.createTitledBorder("Giá phòng"));

        lbidgiaphong.setText("ID giá:");

        tfidgiaphong.setEditable(false);

        lbgiaphong.setText("Giá phòng:");

        lbloaiphong.setText("Loại phòng:");

        cbbloaiphong.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "- Chọn loại phòng -" }));

        lbngayapdung.setText("Ngày áp dụng:");

        btnluugiaphong.setText("Lưu lại");
        btnluugiaphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnluugiaphongActionPerformed(evt);
            }
        });

        btnlamlaigiaphong.setText("Làm lại");
        btnlamlaigiaphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlamlaigiaphongActionPerformed(evt);
            }
        });

        tfgiaphong.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("###,###,#00"))));

        javax.swing.GroupLayout pnleftLayout = new javax.swing.GroupLayout(pnleft);
        pnleft.setLayout(pnleftLayout);
        pnleftLayout.setHorizontalGroup(
            pnleftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnleftLayout.createSequentialGroup()
                .addGroup(pnleftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnleftLayout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addGroup(pnleftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnleftLayout.createSequentialGroup()
                                .addGroup(pnleftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lbgiaphong)
                                    .addComponent(lbidgiaphong))
                                .addGap(18, 18, 18)
                                .addGroup(pnleftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tfgiaphong)
                                    .addGroup(pnleftLayout.createSequentialGroup()
                                        .addComponent(tfidgiaphong, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 95, Short.MAX_VALUE))))
                            .addGroup(pnleftLayout.createSequentialGroup()
                                .addGap(75, 75, 75)
                                .addComponent(btnluugiaphong)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnlamlaigiaphong)
                                .addGap(0, 71, Short.MAX_VALUE))))
                    .addGroup(pnleftLayout.createSequentialGroup()
                        .addGroup(pnleftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(pnleftLayout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addComponent(lbngayapdung)
                                .addGap(18, 18, 18)
                                .addComponent(dpngayapdung, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE))
                            .addGroup(pnleftLayout.createSequentialGroup()
                                .addGap(36, 36, 36)
                                .addComponent(lbloaiphong)
                                .addGap(18, 18, 18)
                                .addComponent(cbbloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(62, 62, 62))
        );
        pnleftLayout.setVerticalGroup(
            pnleftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnleftLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnleftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbidgiaphong)
                    .addComponent(tfidgiaphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnleftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbgiaphong)
                    .addComponent(tfgiaphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnleftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbloaiphong)
                    .addComponent(cbbloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(pnleftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dpngayapdung, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbngayapdung))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(pnleftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnluugiaphong)
                    .addComponent(btnlamlaigiaphong)))
        );

        pnright.setBorder(javax.swing.BorderFactory.createTitledBorder("Loại phòng"));

        lbidloaiphong.setText("ID loại:");

        tfidloaiphong.setEditable(false);

        lbmotaloaiphong.setText("Mô tả loại:");

        lbsonguoio.setText("Số người ở:");

        cbbsonguoio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "- Chọn số người ở -", "1", "2", "3", "4", "5" }));

        btnluuloaiphong.setText("Lưu lại");
        btnluuloaiphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnluuloaiphongActionPerformed(evt);
            }
        });

        btnlamlailoaiphong.setText("Làm lại");
        btnlamlailoaiphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlamlailoaiphongActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnrightLayout = new javax.swing.GroupLayout(pnright);
        pnright.setLayout(pnrightLayout);
        pnrightLayout.setHorizontalGroup(
            pnrightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnrightLayout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addGroup(pnrightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbidloaiphong)
                    .addComponent(lbmotaloaiphong)
                    .addComponent(lbsonguoio))
                .addGap(18, 18, 18)
                .addGroup(pnrightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnrightLayout.createSequentialGroup()
                        .addComponent(btnluuloaiphong)
                        .addGap(18, 18, 18)
                        .addComponent(btnlamlailoaiphong))
                    .addGroup(pnrightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(tfmotaloaiphong)
                        .addComponent(cbbsonguoio, 0, 214, Short.MAX_VALUE)
                        .addComponent(tfidloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(58, Short.MAX_VALUE))
        );
        pnrightLayout.setVerticalGroup(
            pnrightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnrightLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnrightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbidloaiphong)
                    .addComponent(tfidloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnrightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbmotaloaiphong)
                    .addComponent(tfmotaloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnrightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbsonguoio)
                    .addComponent(cbbsonguoio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnrightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnluuloaiphong)
                    .addComponent(btnlamlailoaiphong))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout pnchinhLayout = new javax.swing.GroupLayout(pnchinh);
        pnchinh.setLayout(pnchinhLayout);
        pnchinhLayout.setHorizontalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addComponent(sptbdsloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(sptbdsgiaphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(lbtieude, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnchinhLayout.createSequentialGroup()
                .addContainerGap(443, Short.MAX_VALUE)
                .addComponent(pnright, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27))
            .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnchinhLayout.createSequentialGroup()
                    .addGap(22, 22, 22)
                    .addComponent(pnleft, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(465, Short.MAX_VALUE)))
        );
        pnchinhLayout.setVerticalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addComponent(lbtieude, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(pnright, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sptbdsgiaphong, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sptbdsloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(35, Short.MAX_VALUE))
            .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnchinhLayout.createSequentialGroup()
                    .addGap(60, 60, 60)
                    .addComponent(pnleft, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(323, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnchinh, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(19, 19, 19))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnchinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnluugiaphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnluugiaphongActionPerformed
        if (this.rowEditGiaPhong == -1) {
            if (this.giaPhongControl.Add(this)) {
                this.clearFormGiaPhong();
                this.myFormat.clearTable(this.tbdsgiaphong);
                int row = this.tbdsloaiphong.getSelectedRow();
                if (row != -1) {
                    int idLoaiPhong = Integer.parseInt(this.tbdsloaiphong.getValueAt(row, 1).toString());
                    this.chiTietGiaControl.Show(this.tbdsgiaphong, nhaTroThem.getGPKD(), idLoaiPhong);
                }
            }
        } else {
            if (this.giaPhongControl.Edit(this)) {
                this.clearFormGiaPhong();
                this.reloadDsGiaPhong();
                this.rowEditGiaPhong = -1;
                this.giaPhongOld = null;
                this.ngayApDungOld = null;
                this.loaiPhongOld = null;
            }
        }

    }//GEN-LAST:event_btnluugiaphongActionPerformed

    private void btnlamlaigiaphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlamlaigiaphongActionPerformed
        this.clearFormGiaPhong();
    }//GEN-LAST:event_btnlamlaigiaphongActionPerformed

    private void btnluuloaiphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnluuloaiphongActionPerformed
        if (this.rowEditLoaiPhong == -1) {
            if (this.loaiPhongControl.Add(this)) {
                this.clearFormLoaiPhong();
                this.loaiPhongControl.setCbbLoaiPhong(this.cbbloaiphong);
                this.myFormat.clearTable(this.tbdsloaiphong);
                this.loaiPhongControl.Show(this.tbdsloaiphong);
            }
        } else {
            if (this.loaiPhongControl.Edit(this)) {
                this.clearFormLoaiPhong();
                this.loaiPhongControl.setCbbLoaiPhong(this.cbbloaiphong);
                this.reloadDsLoaiPhong();
                this.reloadDsGiaPhong();
                this.rowEditLoaiPhong = -1;
            }
        }

    }//GEN-LAST:event_btnluuloaiphongActionPerformed

    private void btnlamlailoaiphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlamlailoaiphongActionPerformed
        this.clearFormLoaiPhong();
    }//GEN-LAST:event_btnlamlailoaiphongActionPerformed

    private void menuxoaloaiphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuxoaloaiphongActionPerformed
        int row = this.tbdsloaiphong.getSelectedRow();
        if (row != -1) {
            if (JOptionPane.showConfirmDialog(this, "Xác nhận xóa loại phòng trọ này?", "Xác nhận", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                int idloai = Integer.parseInt(this.tbdsloaiphong.getValueAt(row, 1).toString());
                if (this.loaiPhongControl.Delete(idloai)) {
                    this.loaiPhongControl.setCbbLoaiPhong(this.cbbloaiphong);
                    this.reloadDsLoaiPhong();
                    this.clearFormLoaiPhong();
                } else {
                    JOptionPane.showMessageDialog(this, "Không thể xóa loại phòng này do loại phòng này đã được các nhà trọ khác sử dụng!", "Thông báo", JOptionPane.WARNING_MESSAGE);
                    this.tbdsloaiphong.requestFocus();
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Bạn chưa chọn đối tượng loại phòng trọ để xóa!", "Thông báo", JOptionPane.WARNING_MESSAGE);
            this.tbdsloaiphong.requestFocus();
        }
    }//GEN-LAST:event_menuxoaloaiphongActionPerformed

    private void menuxoagiaphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuxoagiaphongActionPerformed
        int row = this.tbdsgiaphong.getSelectedRow();
        if (row != -1) {
            if (JOptionPane.showConfirmDialog(this, "Xác nhận xóa giá phòng này?", "Xác nhận", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                long gia = Long.parseLong(this.tbdsgiaphong.getValueAt(row, 2).toString());
                String ngayapdung = this.tbdsgiaphong.getValueAt(row, 3).toString();
                int idgiaphong = Integer.parseInt(this.tbdsgiaphong.getValueAt(row, 1).toString());
                try {
                    if (this.chiTietGiaControl.Delete(idgiaphong, gia, ngayapdung)) {
                        this.reloadDsGiaPhong();
                        this.giaPhongControl.Delete(idgiaphong);
                        this.clearFormGiaPhong();
                    }
                } catch (ParseException ex) {
                    Logger.getLogger(f_nhanvien_chunhatro_qlgiaphong.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Bạn chưa chọn đối tượng loại phòng trọ để xóa!", "Thông báo", JOptionPane.WARNING_MESSAGE);
            this.tbdsgiaphong.requestFocus();
        }
    }//GEN-LAST:event_menuxoagiaphongActionPerformed

    private void menusualoaiphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menusualoaiphongActionPerformed
        int row = this.tbdsloaiphong.getSelectedRow();
        if (row != -1) {
            String idPhong = this.tbdsloaiphong.getValueAt(row, 1).toString();
            String moTaLoaiPhong = this.tbdsloaiphong.getValueAt(row, 2).toString();
            String soNguoiO = this.tbdsloaiphong.getValueAt(row, 3).toString();
            this.tfidloaiphong.setText(idPhong);
            this.tfmotaloaiphong.setText(moTaLoaiPhong);
            this.cbbsonguoio.setSelectedItem(soNguoiO);
            this.rowEditLoaiPhong = row;

        } else {
            JOptionPane.showMessageDialog(this, "Bạn chưa chọn đối tượng loại phòng trọ để sửa!", "Thông báo", JOptionPane.WARNING_MESSAGE);
            this.tbdsloaiphong.requestFocus();
        }
    }//GEN-LAST:event_menusualoaiphongActionPerformed

    private void menusuagiaphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menusuagiaphongActionPerformed
        int row = this.tbdsgiaphong.getSelectedRow();
        if (row != -1) {
            int rowLoai = this.tbdsloaiphong.getSelectedRow();
            String idGia = this.tbdsgiaphong.getValueAt(row, 1).toString();
            this.giaPhongOld = this.tbdsgiaphong.getValueAt(row, 2).toString();
            this.ngayApDungOld = this.tbdsgiaphong.getValueAt(row, 3).toString();
            this.loaiPhongOld = (rowLoai != -1) ? this.tbdsloaiphong.getValueAt(rowLoai, 2).toString() : "-Chọn loại phòng-";
            this.tfidgiaphong.setText(idGia);
            this.tfgiaphong.setText(this.giaPhongOld);
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

            try {
                this.dpngayapdung.setDate(sdf.parse(this.ngayApDungOld));
            } catch (ParseException ex) {
                Logger.getLogger(f_nhanvien_chunhatro_qlgiaphong.class.getName()).log(Level.SEVERE, null, ex);
            }
            this.cbbloaiphong.setSelectedItem(this.loaiPhongOld);
            this.rowEditGiaPhong = row;

        } else {
            JOptionPane.showMessageDialog(this, "Bạn chưa chọn đối tượng chi tiết giá phòng để sửa!", "Thông báo", JOptionPane.WARNING_MESSAGE);
            this.tbdsloaiphong.requestFocus();
        }
    }//GEN-LAST:event_menusuagiaphongActionPerformed

    private void clearFormGiaPhong() {
        this.tfidgiaphong.setText(this.giaPhongControl.generateIDGiaPhong() + "");
        this.tfgiaphong.setText(null);
        this.cbbloaiphong.setSelectedIndex(0);
        this.dpngayapdung.setDate(Calendar.getInstance().getTime());
    }

    private void clearFormLoaiPhong() {
        this.tfidloaiphong.setText(this.loaiPhongControl.generateIDLoaiPhong() + "");
        this.tfmotaloaiphong.setText(null);
        this.cbbsonguoio.setSelectedIndex(0);
    }

    public void reloadContent() {
        this.lbtieude.setText("Quản lý giá phòng nhà trọ " + this.nhaTroThem.getTennhatro());
        this.clearFormGiaPhong();
        this.clearFormLoaiPhong();
        this.myFormat.clearTable(this.tbdsgiaphong);
        this.reloadDsLoaiPhong();
        this.tbdsloaiphong.getSelectionModel().addListSelectionListener((ListSelectionEvent event) -> {
            f_nhanvien_chunhatro_qlgiaphong.this.reloadDsGiaPhong();
        });

    }

    private void reloadDsLoaiPhong() {
        this.myFormat.clearTable(this.tbdsloaiphong);
        this.loaiPhongControl.Show(this.tbdsloaiphong);

    }

    private void reloadDsGiaPhong() {
        try {
            int row = this.tbdsloaiphong.getSelectedRow();
            if (row != -1) {
                int idLoaiPhong = Integer.parseInt(this.tbdsloaiphong.getValueAt(row, 1).toString());
                this.myFormat.clearTable(this.tbdsgiaphong);
                this.chiTietGiaControl.Show(this.tbdsgiaphong, nhaTroThem.getGPKD(), idLoaiPhong);
            }
        } catch (Exception ex) {
            //throw exception
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnlamlaigiaphong;
    private javax.swing.JButton btnlamlailoaiphong;
    private javax.swing.JButton btnluugiaphong;
    private javax.swing.JButton btnluuloaiphong;
    public javax.swing.JComboBox cbbloaiphong;
    public javax.swing.JComboBox cbbsonguoio;
    public org.jdesktop.swingx.JXDatePicker dpngayapdung;
    private javax.swing.JLabel lbgiaphong;
    private javax.swing.JLabel lbidgiaphong;
    private javax.swing.JLabel lbidloaiphong;
    private javax.swing.JLabel lbloaiphong;
    private javax.swing.JLabel lbmotaloaiphong;
    private javax.swing.JLabel lbngayapdung;
    private javax.swing.JLabel lbsonguoio;
    private javax.swing.JLabel lbtieude;
    private javax.swing.JMenuItem menusuagiaphong;
    private javax.swing.JMenuItem menusualoaiphong;
    private javax.swing.JMenuItem menuxoagiaphong;
    private javax.swing.JMenuItem menuxoaloaiphong;
    private javax.swing.JPanel pnchinh;
    private javax.swing.JPanel pnleft;
    private javax.swing.JPanel pnright;
    private javax.swing.JPopupMenu popupgiaphong;
    private javax.swing.JPopupMenu popuploaiphong;
    private javax.swing.JScrollPane sptbdsgiaphong;
    private javax.swing.JScrollPane sptbdsloaiphong;
    public javax.swing.JTable tbdsgiaphong;
    public javax.swing.JTable tbdsloaiphong;
    public javax.swing.JFormattedTextField tfgiaphong;
    public javax.swing.JTextField tfidgiaphong;
    public javax.swing.JTextField tfidloaiphong;
    public javax.swing.JTextField tfmotaloaiphong;
    // End of variables declaration//GEN-END:variables
}
